home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2001 May / macformat_103_may_2001.iso / Mac OS X Shareware / Fizilla / Chrome / toolkit.jar / content / global / nsWidgetStateManager.js < prev    next >
Encoding:
JavaScript  |  2001-03-26  |  11.0 KB  |  340 lines

  1. /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  *   Ben "Count XULula" Goodger <ben@netscape.com>
  20.  */
  21.  
  22. /** Presenting widgetStateManager the Third.
  23.  *   a production of ye olde bard Ben Goodger and his Merry XUL Widget Crewe.
  24.  *  =>> MODIFICATIONS MUST BE REVIEWED BY ben@netscape.com!!! <<=
  25.  **/
  26.  
  27. var wsm;
  28.  
  29. function nsWidgetStateManager ( aFrameID )
  30.   {
  31.  
  32.     this.dataManager =
  33.       {
  34.         /** Persisted Data Hash Table
  35.          *  Page_ID -> Element_ID -> Property -> Value
  36.          **/
  37.         pageData: [],
  38.  
  39.         setPageData:
  40.           function ( aPageTag, aDataObject )
  41.             {
  42.               this.pageData[aPageTag] = aDataObject;
  43.             },
  44.  
  45.         getPageData:
  46.           function ( aPageTag )
  47.             {
  48.               if( !(aPageTag in this.pageData) )
  49.                 this.pageData[aPageTag] = [];
  50.               return this.pageData[aPageTag];
  51.             },
  52.  
  53.         setItemData:
  54.           function ( aPageTag, aItemID, aDataObject )
  55.             {
  56.               if( !(aPageTag in this.pageData) )
  57.                 this.pageData[aPageTag] = [];
  58.               this.pageData[aPageTag][aItemID] = aDataObject;
  59.             },
  60.  
  61.         getItemData:
  62.           function ( aPageTag, aItemID )
  63.             {
  64.               if( !(aItemID in this.pageData[aPageTag]) )
  65.                 this.pageData[aPageTag][aItemID] = [];
  66.               return this.pageData[aPageTag][aItemID];
  67.             }
  68.       }
  69.  
  70.     this.contentID    = aFrameID;
  71.  
  72.     wsm               = this;
  73.  
  74.     /** Element Handlers
  75.      *  Provides default get and set handler functions for supported
  76.      *  widgets. Clients can override or add new widgets.
  77.      **/
  78.     this.handlers     =
  79.       {
  80.         menulist:
  81.           {  get: wsm.get_Menulist,    set: wsm.set_Menulist      },
  82.         radiogroup:
  83.           {  get: wsm.get_Radiogroup,  set: wsm.set_Radiogroup    },
  84.         checkbox:
  85.           {  get: wsm.get_Checkbox,    set: wsm.set_Checkbox      },
  86.         textbox:
  87.           {  get: wsm.get_Textbox,     set: wsm.set_Textbox},
  88.         default_handler:
  89.           {  get: wsm.get_Default,     set: wsm.set_Default       }
  90.       }
  91.  
  92.     // extra attributes to scan and save.
  93.     this.attributes   = [];
  94.   }
  95.  
  96. nsWidgetStateManager.prototype =
  97.   {
  98.     get contentArea()
  99.       {
  100.         return window.frames[ this.contentID ];
  101.       },
  102.  
  103.     savePageData:
  104.       function ( aPageTag )
  105.         {
  106.           if (!(aPageTag in this.dataManager.pageData))
  107.             return;
  108.  
  109.           if( 'GetFields' in this.contentArea)
  110.             {
  111.               // save page data based on user supplied function in content area
  112.               var dataObject = this.contentArea.GetFields();
  113.               this.dataManager.setPageData( aPageTag, dataObject );
  114.             }
  115.  
  116.             // Automatic element retrieval. This is done in two ways.
  117.             // 1) if an element id array is present in the document, this is
  118.             //    used to build a list of elements to persist. <-- performant
  119.             // 2) otherwise, all elements with 'wsm_persist' set to true
  120.             //    are persisted <-- non-performant.
  121.            var elements;
  122.            if( '_elementIDs' in this.contentArea )
  123.               {
  124.                 elements = [];
  125.                 for( var i = 0; i < this.contentArea._elementIDs.length; i++ )
  126.                   {
  127.                     var elt = this.contentArea.document.getElementById( this.contentArea._elementIDs[i] );
  128.                     if (elt) {
  129.                       elements[elements.length] = elt;
  130.                     } else {
  131.                       // see bug #40329. People forget this too often, and it breaks Prefs
  132.                       dump("*** FIX ME: '_elementIDs' in '" + this.contentArea.location.href.split('/').pop() +
  133.                            "' contains a reference to a non-existent element ID '" +
  134.                            this.contentArea._elementIDs[i] + "'.\n");
  135.                     }
  136.                   }
  137.               }
  138.             else
  139.               {
  140.                 elements = this.contentArea.document.getElementsByAttribute( "wsm_persist", "true" );
  141.               }
  142.             for( var ii = 0; ii < elements.length; ii++ )
  143.               {
  144.                 var elementID   = elements[ii].id;
  145.                 var elementType = elements[ii].localName;
  146.                 if (!(aPageTag in this.dataManager.pageData) )
  147.                     this.dataManager.pageData[aPageTag] = [];
  148.                 this.dataManager.pageData[aPageTag][elementID] = [];
  149.                 // persist element Type
  150.                 this.dataManager.pageData[aPageTag][elementID].localName = elementType;
  151.                 // persist attributes
  152.                 var get_Func = (elementType in this.handlers) ?
  153.                                 this.handlers[elementType].get :
  154.                                 this.handlers.default_handler.get;
  155.                 this.dataManager.setItemData( aPageTag, elementID, get_Func( elementID ) );
  156.               }
  157.         },
  158.  
  159.     setPageData:
  160.       function ( aPageTag )
  161.         {
  162.           var pageData = this.dataManager.getPageData( aPageTag );
  163.           if( 'SetFields' in this.contentArea )
  164.             {
  165.               this.contentArea.SetFields( pageData );
  166.               return;
  167.             }
  168.  
  169.           for( var elementID in pageData )
  170.             {
  171.               var element = this.contentArea.document.getElementById( elementID );
  172.               if( element )
  173.                 {
  174.                   var elementType = element.localName;
  175.                   var set_Func = (elementType in this.handlers) ?
  176.                                   this.handlers[elementType].set :
  177.                                   this.handlers.default_handler.set;
  178.                   set_Func( elementID, pageData[elementID] );
  179.                 }
  180.             }
  181.         },
  182.  
  183.  
  184.     /** Widget Get/Set Function Implementations
  185.      *  These can be overridden by the client.
  186.      **/
  187.     generic_Set:
  188.       function ( aElement, aDataObject )
  189.         {
  190.           if( aElement )
  191.             {
  192.               for( var property in aDataObject )
  193.                 {
  194.                   aElement.setAttribute( property, aDataObject[property] );
  195.                 }
  196.             }
  197.         },
  198.  
  199.     generic_Get:
  200.       function ( aElement )
  201.         {
  202.           if( aElement )
  203.             {
  204.               var dataObject = [];
  205.               var wsmAttributes = aElement.getAttribute( "wsm_attributes" );
  206.               var attributes = wsm.attributes;              // make a copy
  207.               if( wsmAttributes != "" )
  208.                 {
  209.                   attributes.push( wsmAttributes.split(" ") );  // modify the copy
  210.                 }
  211.               for( var i = 0; i < attributes.length; i++ )
  212.                 {
  213.                   dataObject[attributes[i]] = aElement.getAttribute( attributes[i] );
  214.                 }
  215.               return dataObject;
  216.             }
  217.             return null;
  218.         },
  219.  
  220.     // <menulist>
  221.     set_Menulist:
  222.       function ( aElementID, aDataObject )
  223.         {
  224.           var element = wsm.contentArea.document.getElementById( aElementID );
  225.           // set all generic properties
  226.           wsm.generic_Set( element, aDataObject );
  227.           // set menulist specific properties
  228.           if( 'value' in aDataObject )
  229.             {
  230.               element.selectedItem = element.getElementsByAttribute( "value", aDataObject.value )[0];
  231.             }
  232.         },
  233.  
  234.     get_Menulist:
  235.       function ( aElementID )
  236.         {
  237.           var element     = wsm.contentArea.document.getElementById( aElementID );
  238.           // retrieve all generic attributes
  239.           var dataObject  = wsm.generic_Get( element );
  240.           // retrieve all menulist specific attributes
  241.           if( dataObject )
  242.             {
  243.               dataObject.value = element.getAttribute( "value" );
  244.               return dataObject;
  245.             }
  246.           return null;
  247.         },
  248.  
  249.     // <radiogroup>
  250.     set_Radiogroup:
  251.       function ( aElementID, aDataObject )
  252.         {
  253.  
  254.           var element = wsm.contentArea.document.getElementById( aElementID );
  255.           wsm.generic_Set( element, aDataObject );
  256.           if( 'value' in aDataObject )
  257.             {
  258.               element.selectedItem = element.getElementsByAttribute( "value", aDataObject.value )[0];
  259.             }
  260.         },
  261.  
  262.     get_Radiogroup:
  263.       function ( aElementID )
  264.         {
  265.           var element = wsm.contentArea.document.getElementById( aElementID );
  266.           var dataObject = wsm.generic_Get( element );
  267.           if( dataObject )
  268.             {
  269.               dataObject.value = element.getAttribute( "value" );
  270.               return dataObject;
  271.             }
  272.           return null;
  273.         },
  274.  
  275.     // <textbox>
  276.     set_Textbox:
  277.       function ( aElementID, aDataObject )
  278.         {
  279.           var element = wsm.contentArea.document.getElementById( aElementID );
  280.           wsm.generic_Set( element, aDataObject );
  281.         },
  282.  
  283.     get_Textbox:
  284.       function ( aElementID )
  285.         {
  286.           var element = wsm.contentArea.document.getElementById( aElementID );
  287.           var dataObject = wsm.generic_Get( element );
  288.           if( dataObject )
  289.             {
  290.               dataObject.value = wsm.contentArea.document.getElementById( aElementID ).value;
  291.               return dataObject;
  292.             }
  293.           return null;
  294.         },
  295.  
  296.     // <checkbox>
  297.     set_Checkbox:
  298.       function ( aElementID, aDataObject )
  299.         {
  300.           var element = wsm.contentArea.document.getElementById( aElementID );
  301.           wsm.generic_Set( element, aDataObject );
  302.         },
  303.  
  304.     get_Checkbox:
  305.       function ( aElementID )
  306.         {
  307.           var element = wsm.contentArea.document.getElementById( aElementID );
  308.           var dataObject = wsm.generic_Get( element );
  309.           if( dataObject )
  310.             {
  311.               dataObject.checked = wsm.contentArea.document.getElementById( aElementID ).checked;
  312.               return dataObject;
  313.             }
  314.           return null;
  315.         },
  316.  
  317.     // <default>
  318.     set_Default:
  319.       function ( aElementID, aDataObject )
  320.         {
  321.           var element = wsm.contentArea.document.getElementById( aElementID );
  322.           wsm.generic_Set( element, aDataObject );
  323.         },
  324.  
  325.     get_Default:
  326.       function ( aElementID )
  327.         {
  328.           var element = wsm.contentArea.document.getElementById( aElementID );
  329.           var dataObject = wsm.generic_Get( element );
  330.           return dataObject ? dataObject : null;
  331.         }
  332.   }
  333.  
  334.  
  335. /* it will be dark soon */
  336. /* MANOS MADE ME PERMANENT! */
  337. /* there is no way out of here */
  338.  
  339.  
  340.